﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace TimKen.DataAccess
{
    /// <summary>
    /// 数据访问类LinkMan。
    /// </summary>
    public class LinkManDAO
    {
        public LinkManDAO()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string CntctCode)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@CntctCode", SqlDbType.NVarChar,50)};
            parameters[0].Value = CntctCode;

            int result = DbHelperSQL.RunProcedure("UP_LinkMan_Exists", parameters, out rowsAffected);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  增加一条数据
        /// </summary>
        public void Add(TimKen.DataEntity.LinkMan model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@CntctCode", SqlDbType.NVarChar,11),
					new SqlParameter("@CardCode", SqlDbType.NVarChar,15),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Telephone", SqlDbType.NVarChar,20),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Fax", SqlDbType.NVarChar,20),
					new SqlParameter("@Address", SqlDbType.NVarChar,100),
					new SqlParameter("@Email", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.CntctCode;
            parameters[1].Value = model.CardCode;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Telephone;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.Fax;
            parameters[6].Value = model.Address;
            parameters[7].Value = model.Email;

            DbHelperSQL.RunProcedure("UP_LinkMan_ADD", parameters, out rowsAffected);
        }

        /// <summary>
        ///  更新一条数据
        /// </summary>
        public void Update(TimKen.DataEntity.LinkMan model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@CntctCode", SqlDbType.NVarChar,11),
					new SqlParameter("@CardCode", SqlDbType.NVarChar,15),
					new SqlParameter("@Name", SqlDbType.NVarChar,50),
					new SqlParameter("@Telephone", SqlDbType.NVarChar,20),
					new SqlParameter("@Mobile", SqlDbType.NVarChar,50),
					new SqlParameter("@Fax", SqlDbType.NVarChar,20),
					new SqlParameter("@Address", SqlDbType.NVarChar,100),
					new SqlParameter("@Email", SqlDbType.NVarChar,100)};
            parameters[0].Value = model.CntctCode;
            parameters[1].Value = model.CardCode;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Telephone;
            parameters[4].Value = model.Mobile;
            parameters[5].Value = model.Fax;
            parameters[6].Value = model.Address;
            parameters[7].Value = model.Email;

            DbHelperSQL.RunProcedure("UP_LinkMan_Update", parameters, out rowsAffected);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string CntctCode)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@CntctCode", SqlDbType.NVarChar,50)};
            parameters[0].Value = CntctCode;

            DbHelperSQL.RunProcedure("UP_LinkMan_Delete", parameters, out rowsAffected);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TimKen.DataEntity.LinkMan GetModel(string CntctCode)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@CntctCode", SqlDbType.NVarChar,50)};
            parameters[0].Value = CntctCode;

            TimKen.DataEntity.LinkMan model = new TimKen.DataEntity.LinkMan();
            DataSet ds = DbHelperSQL.RunProcedure("UP_LinkMan_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.CntctCode = ds.Tables[0].Rows[0]["CntctCode"].ToString();
                model.CardCode = ds.Tables[0].Rows[0]["CardCode"].ToString();
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Telephone = ds.Tables[0].Rows[0]["Telephone"].ToString();
                model.Mobile = ds.Tables[0].Rows[0]["Mobile"].ToString();
                model.Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                model.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select CntctCode,CardCode,Name,Telephone,Mobile,Fax,Address,Email ");
            strSql.Append(" FROM LinkMan ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "LinkMan";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}
